﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ADEVSO.Util
{
    public enum Hour
    {
        None = 0,
        AM1200 = 1,
        AM1230 = 2,
        AM0100 = 3,
        AM0130 = 4,
        AM0200 = 5,
        AM0230 = 6,
        AM0300 = 7,
        AM0330 = 8,
        AM0400 = 9,
        AM0430 = 10,
        AM0500 = 11,
        AM0530 = 12,
        AM0600 = 13,
        AM0630 = 14,
        AM0700 = 15,
        AM0730 = 16,
        AM0800 = 17,
        AM0830 = 18,
        AM0900 = 19,
        AM0930 = 20,
        AM1000 = 21,
        AM1030 = 22,
        AM1100 = 23,
        AM1130 = 24,
        PM1200 = 25,
        PM1230 = 26,
        PM0100 = 27,
        PM0130 = 28,
        PM0200 = 29,
        PM0230 = 30,
        PM0300 = 31,
        PM0330 = 32,
        PM0400 = 33,
        PM0430 = 34,
        PM0500 = 35,
        PM0530 = 36,
        PM0600 = 37,
        PM0630 = 38,
        PM0700 = 39,
        PM0730 = 40,
        PM0800 = 41,
        PM0830 = 42,
        PM0900 = 43,
        PM0930 = 44,
        PM1000 = 45,
        PM1030 = 46,
        PM1100 = 47,
        PM1130 = 48
    }

    public static class Extensions
    {
        public static DateTime ToDateTime(this Hour hour)
        {
            return Convert.ToDateTime(hour.ToStringHour());
        }

        public static String ToStringHour(this Hour hour)
        {
            String hourFormated = hour.ToString();
            if (hourFormated.Length != 6)
                hourFormated = String.Empty;
            else hourFormated = hourFormated.Substring(2, 2) + ":" + hourFormated.Substring(4, 2) + " " + hourFormated.Substring(0, 2);
            return hourFormated;
        }

        public static Hour ToHour(this String hour)
        {
            for (int i = 1; i <= 48; i++)
                if (((Hour)i).ToStringHour().Equals(hour) || ((Hour)i).ToString().Equals(hour))
                    return (Hour)i;
            return Hour.AM0100;
        }

        public static Hour Add(this Hour hour, int added)
        {
            Hour hours = (Hour) ((int) hour + added);
            return hours;
        }

        public static List<String> AllHours(this Hour hour)
        {
            List<String> hours = new List<string>();
            for (int i = 1; i <= 48; i++)
                hours.Add(((Hour)i).ToStringHour());
            return hours;
        }

        public static List<Hour> RangeHours(this Hour hour, Hour startHour, Hour endHour, bool halfHours)
        {
            List<Hour> hours = new List<Hour>();
            for (int i = 1; i <= 48; i++)
            {
                if (startHour <= ((Hour)i))
                {
                    if (!halfHours && (i % 2 == 0)) continue;
                    hours.Add((Hour)i);
                    if (endHour == ((Hour)i))
                        break;
                }
            }
            return hours;
        }

        public static Int32 GetHour(this Hour hour)
        {
            Int32 result = Convert.ToInt32(hour.ToString().Substring(2, 2));
            if ((hour.ToString().Substring(0, 2).Equals("PM") && hour != Hour.PM1200 && hour != Hour.PM1230) || hour == Hour.AM1200 || hour == Hour.AM1230)
                result = result + 12;
            return result;
        }

        public static Int32 GetMinutes(this Hour hour)
        {
            Int32 result = Convert.ToInt32(hour.ToString().Substring(4, 2));
            return result;
        }
    }
}
